/**************************************************************关闭弹出层**************************************************************/
function closeDialog() {
	$(".stdmod-background").remove();
	$(".stdmod-dialog").remove();
	//重置
	$(".modules .barbox").eq($("#current_index").val()).find('[data-module-index]').replaceWith($("#current_html").children());
	$("#current_index").val("");
	$("#current_html").html("");
}

$.fn.resizeImg = function(o) {
	var base = this;
	var thisImg = $(base).find('img');
	var imgWidth = $(thisImg).width();
	var imgHeight = $(thisImg).height();
	var imgPercent = (imgWidth/640)*100;
	
	var reLeft = $(base).find('.re-left');
	var reRight = $(base).find('.re-right');
	$(reLeft).unbind('mousedown').mousedown(function() {
		
	});
	$(reRight).unbind('mousedown').mousedown(function(e) {
		var eX = e.pageX; 
		var eY = e.pageY;
		$(document).bind("mousemove",function(e) {
			var _eX = e.pageX - eX; 
			var _eY = e.pageY - eY;
			resize(_eX,_eY);
		});
		$(document).bind("mouseup",function(){
			$(document).unbind("mousemove");
			$(document).unbind("mouseup");
		});
	});
	var resize = function(x,y) {
		var _imgWidth,_imgHeight;
		if(x > y) {
			_imgWidth = imgWidth + x;
			_imgHeight = _imgWidth*imgHeight/imgWidth;
		} else {
			_imgHeight = imgHeight + y;
			_imgWidth = _imgHeight*imgWidth/imgHeight;
		}
		if(!!o.m && o.m == 'p') {
			if(_imgWidth > 640) return;
			imgPercent = (_imgWidth/640)*100;
		}
		thisImg.attr('width', _imgWidth);
		thisImg.attr('height', _imgHeight);
		
		base.css({width:_imgWidth, height:_imgHeight});
		setState(_imgWidth,_imgHeight,imgPercent);
	};
	var setState = function(w,h,p) {
		if(!!o) {
			if(!!o.w) $(o.w).html(parseInt(w) + 'px');
			if(!!o.h) $(o.h).html(parseInt(h) + 'px');
			if(!!o.p) $(o.p).html(parseInt(p) + '%');
		}
	}
	setState(imgWidth, imgHeight, imgPercent);
};

$.fn.resizeListBox = function() {
	var $base = $(this);
	var $barbox = $base.parent('.barbox');
	
	var $container = $barbox.parent('.container');
	var contWidth = $container.width();
	var contHeight = $container.height();
	
	var boxWidth = $barbox.width();
	var boxHeight = $barbox.height();
	var boxTop = $barbox.position().top;
	var boxLeft = $barbox.position().left;
	
	var $listbox = $barbox.find('.resize');
	var $barmask = $barbox.find('.barmain > .barmask');
	
	var $reRight = $base.find('.re-right');
	var $reLeft = $base.find('.re-left');
	var $topRight = $base.find('.top-right');
	var $topLeft = $base.find('.top-left');
	var $midRight = $base.find('.mid-right');
	var $midLeft = $base.find('.mid-left');
	
	var mousefn = function($els, type) {
		$els.unbind('mousedown').mousedown(function(e) {
			var eX = e.pageX; 
			var eY = e.pageY;
			$(document).unbind('mousemove').bind("mousemove",function(e) {
				var _eX = e.pageX - eX; 
				var _eY = e.pageY - eY;
				resize(_eX, _eY, type);
			});
			$(document).unbind('mouseup').bind("mouseup",function(){
				$(document).unbind("mousemove");
				$(document).unbind("mouseup");
			});
		});
	}
	
	var resize = function(x, y, type) {
		var _boxWidth, _boxHeight, _boxTop, _boxLeft;
		if(type == 'br') {
			_boxTop = boxTop;
			_boxHeight = boxHeight + y;
			if(_boxHeight + _boxTop >= contHeight) {
				_boxHeight = contHeight - _boxTop;
			}
			
			_boxLeft = boxLeft;
			_boxWidth = boxWidth + x;
			if(_boxWidth + _boxLeft >= contWidth) {
				_boxWidth = contWidth - boxLeft;
			}
		}
		if(type == 'bl') {
			_boxTop = boxTop;
			_boxHeight = boxHeight + y;
			if(_boxHeight + _boxTop >= contHeight) {
				_boxHeight = contHeight - _boxTop;
			}
			
			_boxLeft = boxLeft + x;
			if(_boxLeft < 0) {
				_boxLeft = 0;
				_boxWidth = boxWidth;
			} else {
				_boxWidth = boxWidth - x;
				if(_boxWidth <= 0) {
					_boxLeft = boxLeft + boxWidth;
				}
			}
		}
		
		if(type == 'mr') {
			_boxTop = boxTop;
			_boxHeight = boxHeight;
			
			_boxLeft = boxLeft;
			_boxWidth = boxWidth + x;
			if(_boxWidth + _boxLeft >= contWidth) {
				_boxWidth = contWidth - boxLeft;
			}
		}
		if(type == 'ml') {
			_boxTop = boxTop;
			_boxHeight = boxHeight;
			
			_boxLeft = boxLeft + x;
			if(_boxLeft < 0) {
				_boxLeft = 0;
				_boxWidth = boxWidth;
			} else {
				_boxWidth = boxWidth - x;
				if(_boxWidth <= 0) {
					_boxLeft = boxLeft + boxWidth;
				}
			}
		}
		
		if(type == 'tr') {
			_boxTop = boxTop + y;
			if(_boxTop < 0) {
				_boxTop = 0;
				_boxHeight = boxHeight;
			} else {
				_boxHeight = boxHeight - y;
				if(_boxHeight <= 0) {
					_boxTop = boxTop + boxHeight;
				}
			}
			
			_boxLeft = boxLeft;
			_boxWidth = boxWidth + x;
			if(_boxWidth + _boxLeft >= contWidth) {
				_boxWidth = contWidth - boxLeft;
			}
		}
		
		if(type == 'tl') {
			_boxTop = boxTop + y;
			if(_boxTop < 0) {
				_boxTop = 0;
				_boxHeight = boxHeight;
			} else {
				_boxHeight = boxHeight - y;
				if(_boxHeight <= 0) {
					_boxTop = boxTop + boxHeight;
				}
			}
			_boxLeft = boxLeft + x;
			if(_boxLeft < 0) {
				_boxLeft = 0;
				_boxWidth = boxWidth;
			} else {
				_boxWidth = boxWidth - x;
				if(_boxWidth <= 0) {
					_boxLeft = boxLeft + boxWidth;
				}
			}
		}
		
		$barbox.css({width:_boxWidth, height:_boxHeight, left:_boxLeft, top:_boxTop});
		$barmask.css({width:_boxWidth, height:_boxHeight});
		$listbox.css({width:_boxWidth, height:_boxHeight});
	};
	
	mousefn($reRight, 'br');
	mousefn($reLeft, 'bl');
	mousefn($midRight, 'mr');
	mousefn($midLeft, 'ml');
	mousefn($topRight, 'tr');
	mousefn($topLeft, 'tl');
};


//* 文字列表添加 */
$.fn.initNewsEdit = function() {
	var $base = $(this);
	var $list = $base.find('.setfontlink > ul');
	var $addlist = $base.find('.addtextbox .addtext');
	$addlist.click(function() {
		var innerHtml = 
			'<li>'+
				'<div class="f-l editfont">'+
					'<span>文字内容：</span>'+
					'<input type="text" name="news_title" id="news_title" class="stext" value="输入文字内容" />'+
				'</div>'+
				'<div class="f-l editfont">'+
					'<span>链接地址：</span>'+
					'<div class="selectlink">'+
						'<input type="text" name="news_url" id="news_url" class="stext" />'+
						'<ul class="sublinkbox">'+
							'<li data-url="/">网站首页</li>'+
							'<li data-url="/about">关于我们</li>'+
							'<li data-url="/contact">联系我们</li>'+
						'</ul>'+
					'</div>'+
				'</div>'+
				'<select class="f-l linkopen" name="news_opentype" id="news_opentype">'+
					'<option value="">目前窗口打开</option>'+
					'<option value="_blank">新的窗口打开</option>'+
				'</select>'+
				'<a title="删除" class="canceltext" href="javascript:;">删除</a>'+
			'</li>';
		$list.append(innerHtml);
		initFn();
	});
	var initFn = function() {
		$list.find('li .canceltext').unbind('click').click(function() {
			$(this).parents('li').remove();
		});
		$base.find('.selectlink > input').unbind('focus').focus(function() {
			$(this).parents('.selectlink').find('.sublinkbox').show();
			$(this).parents('li').siblings('li').find('.sublinkbox').hide();

		});

		$base.find('.selectlink > input').unbind('blur').blur(function() {
			setTimeout(function(){
				$(this).parents('.selectlink').find('.sublinkbox').hide();
			},1)
		});

		$base.find('.selectlink .sublinkbox > li').unbind('click').click(function() {
			var thisValue = $(this).attr("data-url");
			$(this).parents('.selectlink').find('input').val(thisValue);
			$(this).parents('.sublinkbox').hide();
		});
	};
	initFn();
};

//* 链接获取 */
$.fn.initPicEdit = function() {
	var $pub = $(this);
	
	var initLb = function(){
		$pub.find('.p-link > input').focus(function() {
			$(this).parents('.p-link').find('.sublinkbox').show();
		});
		$pub.find('.p-link > input').blur(function() {
			$(this).parents('.p-link').find('.sublinkbox').hide();
		});
		$pub.find('.p-link .sublinkbox > li').click(function() {
			var thisValue = $(this).attr("data-url");
			$(this).parents('.p-link').find('input').val(thisValue);
			$(this).parents('.sublinkbox').hide();
		});
	};
	initLb();
};


/* 导航菜单添加修改 */
$.fn.initNavEdit = function() {
	var base = this;
	var btnAddFirst = base.find('.navadd').find('.addsort');
	var rangeBox = base.find('.rangebox');
	var rangeValue = rangeBox.find('.range-value');
	var setMove = function(e, rangeSlider) {
		var btnHandle = rangeSlider.find('a');
		var bhWidth = btnHandle.width();
		var moveX = (e.pageX - bhWidth/2) - rangeSlider.offset().left;
		moveX = moveX < 0 ? 0 : moveX;
		if(moveX + bhWidth > rangeSlider.width() - 2) {
			moveX = (rangeSlider.width() - 2) - bhWidth;
		}
		rangeValue.val(moveX);
		btnHandle.css({left: moveX});
	};
	rangeBox.find('.range-slider').mousedown(function(e) {
		var rangeSlider = $(this);
		setMove(e, rangeSlider);
		$(document).bind("mousemove",function(e) {
			setMove(e, rangeSlider);
		});
		$(document).bind("mouseup",function(){
			$(document).unbind("mousemove");
			$(document).unbind("mouseup");
		});
		
	});
	var navTable = base.find('.navtable');
	//Expanded Collapsed
	var btnToggle = base.find('.navadd').find('.togglesort');
	btnToggle.unbind('click').click(function() {
		if($(this).html() == '展开所有分类') {
			$(this).html('收缩所有分类');
			toggles('-');
		} else {
			$(this).html('展开所有分类');
			toggles('+');
		}
	});
	
	var toggles = function(type) {
		var navList = navTable.find('tbody');
		navList.each(function() {
			var catsup = $(this).find('.cat-sup');
			var catsub = $(this).find('.cat-sub');
			var iToggle = catsup.find('i');
			if(type == '+') {
				if(iToggle.attr('class') == 'closednav') {
					iToggle.removeClass('closednav');
					catsub.hide();
				}
			}
			if(type == '-') {
				if(iToggle.attr('class') != 'closednav') {
					iToggle.addClass('closednav');
					catsub.show();
				}
			}
		});
	}
	// 添加一级菜单
	btnAddFirst.unbind('click').click(function() {
		var newFirst = 
			'<tbody>'+
				'<tr class="cat-sup">'+
					'<td class="td-0"><i></i></td>'+
					'<td class="td-1"><input type="text" maxlength="20" value="新一级菜单" class="stext" /></td>'+
					'<td class="td-2"><a class="move-btn move-up" href="javascript:;">上移</a></td>'+
					'<td class="td-3"><a class="move-btn move-down" href="javascript:;">上移</a></td>'+
					'<td class="td-4"><a class="move-btn move-delete" href="javascript:;">删除</a></td>'+
				'</tr>'+
				'<tr class="cat-sub add-sub" style="display:none;">'+
					'<td class="td-0"></td>'+
					'<td class="td-1"><a class="add-cat" href="javascript:;">添加二级菜单</a></td>'+
					'<td class="td-2"></td>'+
					'<td class="td-3"></td>'+
					'<td class="td-4"></td>'+
				'</tr>'+
			'</tbody>';
		navTable.find('thead').after(newFirst);
		initFn();
	});
	// 初始化操作函数
	var initFn = function() {
		var navList = navTable.find('tbody');
		navList.each(function() {
			var catsup = $(this).find('.cat-sup');
			var catsub = $(this).find('.cat-sub');
			catsup.find('i').unbind('click').click(function() {
				if($(this).attr('class') == 'closednav') {
					$(this).removeClass('closednav');
					catsub.hide();
				} else {
					$(this).addClass('closednav');
					catsub.show();
				}
			});
			var supParent = $(this);
			catsup.find('.move-btn.move-up').unbind('click').click(function() {
				if(navList.index(supParent) == 0) return;
				supParent.prev().before(supParent);
				initFn();
			});
			catsup.find('.move-btn.move-down').unbind('click').click(function() {
				if(navList.index(supParent) == navList.length - 1) return;
				supParent.next().after(supParent);
				initFn();
			});
			catsup.find('.move-btn.move-delete').unbind('click').click(function() {
				if(confirm('确定删除 "' + catsup.find('input').val() + '" 节点？')) {
					supParent.remove();
					initFn();
				}
			});
			catsub.each(function() {
				var subParent = $(this);
				subParent.find('.move-btn.move-up').unbind('click').click(function() {
					if(catsub.index(subParent) == 0) return;
					subParent.prev().before(subParent);
					initFn();
				});
				subParent.find('.move-btn.move-down').unbind('click').click(function() {
					if(catsub.index(subParent) == catsub.length - 2) return;
					subParent.next().after(subParent);
					initFn();
				});
				subParent.find('.move-btn.move-delete').unbind('click').click(function() {
					if(confirm('确定删除 "' + subParent.find('input').val() + '" 节点？')) {
						subParent.remove();
						initFn();
					}
				});
			});
			var secParent = $(this).find('.cat-sub.add-sub');
			secParent.find('.add-cat').unbind('click').click(function() {
				var newSecond = 
					'<tr class="cat-sub">'+
						'<td class="td-0"></td>'+
						'<td class="td-1"><input type="text" maxlength="20" value="新二级菜单" class="stext" /></td>'+
						'<td class="td-2"><a class="move-btn move-up" href="javascript:;">上移</a></td>'+
						'<td class="td-3"><a class="move-btn move-down" href="javascript:;">上移</a></td>'+
						'<td class="td-4"><a class="move-btn move-delete" href="javascript:;">删除</a></td>'+
					'</tr>';
				secParent.before(newSecond);
				initFn();
			});
		});
		navList.each(function() {
			var lineTr = $(this).find('tr');
			lineTr.each(function() {
				if($(this).attr('class') == 'cat-sup' || $(this).attr('class') == 'cat-sub' ) {
					$(this).hover(function() {
						$(this).attr('bgcolor', '#D9E5F7');
					},function() {
						$(this).removeAttr('bgcolor');
					});
				}
			});
		});
	};
	initFn();
};